package hczc.longcai.com.hccrowdfunding.home.second_invest.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.List;

import hczc.longcai.com.hccrowdfunding.R;
import hczc.longcai.com.hccrowdfunding.activity.BaseActivity;
import hczc.longcai.com.hccrowdfunding.application.BaseApplication;
import hczc.longcai.com.hccrowdfunding.home.second_invest.beans.ProjectDetail;
import hczc.longcai.com.hccrowdfunding.home.second_invest.beans.details_items.DetailReturn;

/**
 * Created by Administrator on 2016/1/14.
 */
public class ProjectDetailsAdapter extends BaseAdapter implements View.OnClickListener {

    private Context context;
    private List<ProjectDetail> list;
    private CallBack callBack;

    private int choose_state;

    public ProjectDetailsAdapter(Context context, List<ProjectDetail> list, CallBack callBack) {
        this.context = context;
        this.list = list;
        this.callBack = callBack;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public int getViewTypeCount() {
        return 12;
    }

    @Override
    public int getItemViewType(int position) {
        return list.get(position).getType();
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        int type = getItemViewType(i);
        if (view == null) {
            holder = new ViewHolder();
            switch (type) {
                /**
                 * 加载标题界面
                 */
                case 1:
                    view = LayoutInflater.from(context).inflate(R.layout.item_detail_listview_head, null);
                    BaseApplication.scaleScreenHelper.loadView(((ViewGroup) view));
                    holder.title = ((TextView) view.findViewById(R.id.detail_listview_title));
                    holder.cyzcrs = ((TextView) view.findViewById(R.id.detail_listview_cyzcrs));
                    holder.zcz = ((TextView) view.findViewById(R.id.detail_listview_zcz));
                    holder.time = ((TextView) view.findViewById(R.id.detail_listview_sjhsy_time));
                    holder.earnings = ((TextView) view.findViewById(R.id.detail_listview_sjhsy_earnings));
                    view.setTag(holder);
                    break;
                /**
                 * 加载目标金额、已筹金额界面
                 */
                case 2:
                    view = LayoutInflater.from(context).inflate(R.layout.item_detail_listview_second, null);
                    BaseApplication.scaleScreenHelper.loadView(((ViewGroup) view));
                    holder.mbje = ((TextView) view.findViewById(R.id.detail_listview_mbje));
                    holder.ycje = ((TextView) view.findViewById(R.id.detail_listview_ycje));
                    view.setTag(holder);
                    break;
                /**
                 * 在没有回报时，加载众筹最小单位、总份数等界面
                 */
                case 3:
                    view = LayoutInflater.from(context).inflate(R.layout.item_detail_listview_third, null);
                    BaseApplication.scaleScreenHelper.loadView(((ViewGroup) view));
                    holder.zczxdw = ((TextView) view.findViewById(R.id.detail_listview_zczxdw));
                    holder.zfs = ((TextView) view.findViewById(R.id.detail_listview_zfs));
                    holder.yqnhsyl = ((TextView) view.findViewById(R.id.detail_listview_yqnhsyl));
                    holder.syfs = ((TextView) view.findViewById(R.id.detail_listview_syfs));
                    view.setTag(holder);
                    break;
                /**
                 * 加载发起人界面
                 */
                case 4:
                    view = LayoutInflater.from(context).inflate(R.layout.item_detail_listview_fourth,null);
                    BaseApplication.scaleScreenHelper.loadView(((ViewGroup) view));
                    holder.fqrHead = ((ImageView) view.findViewById(R.id.detail_listview_fqr_head));
                    holder.fqrID = ((TextView) view.findViewById(R.id.detail_listview_fqr_name));
                    holder.attention = ((ImageView) view.findViewById(R.id.detail_listview_attention));
                    view.setTag(holder);
                    break;
                /**
                 * 加载期限及点赞界面
                 */
                case 5:
                    view = LayoutInflater.from(context).inflate(R.layout.item_detail_listview_fifth,null);
                    BaseApplication.scaleScreenHelper.loadView(((ViewGroup) view));
                    holder.qixian = ((TextView) view.findViewById(R.id.detail_listview_qixian));
                    holder.zan = ((ImageView) view.findViewById(R.id.detail_listview_zan));
                    holder.zanshu = ((TextView) view.findViewById(R.id.detail_listview_zanshu));
                    holder.zanLayout = ((RelativeLayout) view.findViewById(R.id.detail_listview_zan_layout));
                    view.setTag(holder);
                    break;
                /**
                 * 加载项目说明
                 */
                case 6:
                    view = LayoutInflater.from(context).inflate(R.layout.item_detail_listview_sixth,null);
                    BaseApplication.scaleScreenHelper.loadView(((ViewGroup) view));
                    holder.xmsm = ((RelativeLayout) view.findViewById(R.id.detail_listview_xmsm));
                    view.setTag(holder);
                    break;
                /**
                 * 加载项目进展界面
                 */
                case 7:
                    view = LayoutInflater.from(context).inflate(R.layout.item_detail_listview_seventeen,null);
                    BaseApplication.scaleScreenHelper.loadView(((ViewGroup) view));
                    holder.xmjzTitle = ((TextView) view.findViewById(R.id.detail_listview_xmjz_text));
                    holder.xmjzNewest = ((TextView) view.findViewById(R.id.detail_listview_xmjz_newest));
                    holder.allXmjz = ((TextView) view.findViewById(R.id.detail_listview_xmjz_all));
                    view.setTag(holder);
                    break;
                /**
                 * 加载评论界面
                 */
                case 8:
                    view = LayoutInflater.from(context).inflate(R.layout.item_detail_listview_eighth,null);
                    BaseApplication.scaleScreenHelper.loadView(((ViewGroup) view));
                    holder.commenqHead = ((ImageView) view.findViewById(R.id.detail_listview_comment_head));
                    holder.commentID = ((TextView) view.findViewById(R.id.detail_listview_comment_name));
                    holder.commentTime = ((TextView) view.findViewById(R.id.detail_listview_comment_time));
                    holder.commentDetail = ((TextView) view.findViewById(R.id.detail_listview_comment_detail));
                    holder.allComment = ((TextView) view.findViewById(R.id.detail_listview_comment_all));
                    view.setTag(holder);
                    break;
                /**
                 * 分隔线
                 */
                case 9:
                    view = LayoutInflater.from(context).inflate(R.layout.item_detail_listview_fengexian,null);
                    BaseApplication.scaleScreenHelper.loadView(((ViewGroup) view));
                    view.setTag(holder);
                    break;
                /**
                 * 如果是项目回报页面，则加载次界面
                 */
                case 10:
                    view = LayoutInflater.from(context).inflate(R.layout.item_detail_listview_nineth,null);
                    BaseApplication.scaleScreenHelper.loadView(((ViewGroup) view));
                    holder.support = ((TextView) view.findViewById(R.id.detail_listview_return_support));
                    holder.allSupporter = ((TextView) view.findViewById(R.id.detail_listview_return_support_all));
                    holder.residueSupperter = ((TextView) view.findViewById(R.id.detail_listview_return_support_residue));
                    holder.returnDetail = ((TextView) view.findViewById(R.id.detail_listview_return_detail));
                    holder.returnPrice = ((TextView) view.findViewById(R.id.detail_listview_return_price));
                    holder.sendTime = ((TextView) view.findViewById(R.id.detail_listview_return_sendtime));
                    holder.choose = ((ImageView) view.findViewById(R.id.detail_listview_return_choose));
                    holder.chooseLayout = ((RelativeLayout) view.findViewById(R.id.detail_listview_return_choose_layout));
                    view.setTag(holder);
                    break;
                /**
                 * 选择回报的标题
                 */
                case 11:
                    view = LayoutInflater.from(context).inflate(R.layout.item_detail_lietview_nineth_head,null);
                    BaseApplication.scaleScreenHelper.loadView(((ViewGroup) view));
                    view.setTag(holder);
                    break;

            }
        }
        holder = ((ViewHolder) view.getTag());
        ProjectDetail detail = list.get(i);
        switch (type) {
            case 1:
                holder.title.setText(detail.getTitle());
                break;
            case 2:
                break;
            case 3:
                break;
            case 4:
                holder.attention.setOnClickListener(this);
                break;
            case 5:
//                holder.zan.setOnClickListener(this);
                holder.zanLayout.setOnClickListener(this);
                break;
            case 6:
                holder.xmsm.setOnClickListener(this);
                break;
            case 7:
                holder.allXmjz.setOnClickListener(this);
                break;
            case 8:
                holder.allComment.setOnClickListener(this);
                break;

            case 10:
                holder.chooseLayout.setOnClickListener(this);
                DetailReturn detailReturn = detail.getaReturn();
                choose_state = detailReturn.getIndex();

                holder.chooseLayout.setTag(choose_state);
                if(detailReturn != null) {
                    if (detailReturn.isSelected()) {
                        holder.choose.setSelected(true);
                    }
                    else {
                        holder.choose.setSelected(false);
                    }
                }
                break;

            case 11:

                break;



        }

        return view;
    }

    public interface CallBack{
        void click(View view);
    }

    @Override
    public void onClick(View view) {
        callBack.click(view);
    }

    class ViewHolder {
        TextView title,cyzcrs,zcz,time,earnings,mbje,ycje,zczxdw,zfs,yqnhsyl,syfs,fqrID,qixian,zanshu,xmjzTitle,xmjzNewest,allXmjz,commentID,commentTime,commentDetail,allComment,support,allSupporter,residueSupperter,returnDetail,returnPrice,sendTime;
        ImageView fqrHead,attention,zan,commenqHead,choose;
        RelativeLayout xmsm,chooseLayout,zanLayout;
    }
}
